<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改':'添加'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">
        <el-form-item
          label=":"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.status"
            placeholder="请输入"
            clearable/>
        </el-form-item>
        <el-form-item label=":">
          <el-date-picker
            type="datetime"
            class="ele-fluid"
            v-model="form.syncTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择"/>
        </el-form-item>
        <el-form-item
          label="合同类别:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.type"
            placeholder="请输入合同类别"
            clearable/>
        </el-form-item>
        <el-form-item
          label="合同属性:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.attribute"
            placeholder="请输入合同属性"
            clearable/>
        </el-form-item>
        <el-form-item
          label="OA合同审级（一般合同）:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.commonLevel1"
            placeholder="请输入OA合同审级（一般合同）"
            clearable/>
        </el-form-item>
        <el-form-item
          label="OA合同审级（医药、天一-重大合同）:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.commonLevel2"
            placeholder="请输入OA合同审级（医药、天一-重大合同）"
            clearable/>
        </el-form-item>
        <el-form-item
          label="OA合同审级（医药天一除外-重大合同）:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.commonLevel3"
            placeholder="请输入OA合同审级（医药天一除外-重大合同）"
            clearable/>
        </el-form-item>
        <el-form-item
          label="管理属性:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.manageAttribute"
            placeholder="请输入管理属性"
            clearable/>
        </el-form-item>
        <el-form-item
          label="OA管理属性:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.oaManageAttribute"
            placeholder="请输入OA管理属性"
            clearable/>
        </el-form-item>
        <el-form-item
          label="签约授权人:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.authorizer"
            placeholder="请输入签约授权人"
            clearable/>
        </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'FormtableMainContractMapEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        status: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        syncTime: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入合同类别', trigger: 'blur'}
        ],
        attribute: [
          {required: true, message: '请输入合同属性', trigger: 'blur'}
        ],
        commonLevel1: [
          {required: true, message: '请输入OA合同审级（一般合同）', trigger: 'blur'}
        ],
        commonLevel2: [
          {required: true, message: '请输入OA合同审级（医药、天一-重大合同）', trigger: 'blur'}
        ],
        commonLevel3: [
          {required: true, message: '请输入OA合同审级（医药天一除外-重大合同）', trigger: 'blur'}
        ],
        manageAttribute: [
          {required: true, message: '请输入管理属性', trigger: 'blur'}
        ],
        oaManageAttribute: [
          {required: true, message: '请输入OA管理属性', trigger: 'blur'}
        ],
        authorizer: [
          {required: true, message: '请输入签约授权人', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/formtablemaincontractmap/edit' : '/formtablemaincontractmap/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
